package Food_util;



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBHelp {
	//数据库连接的方法
	public static Connection getConnection() {	
		String DBname = "food"; 	//所连接数据库名称
		String DBuser = "root";		//数据库账号
		String DBpsw = "root"; 	//数据库密码
		
		Connection con=null;
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(  
				    "jdbc:mysql://localhost:3306/"+DBname+"?useUnicode=true&characterEncoding=utf8",  
				    DBuser, DBpsw  
				);
			System.out.println("连接数据库成功");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.out.println("连接数据库失败");
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("连接数据库失败");
		}
		return con;
	}
	
	//数据库关闭的方法
	public static void close(ResultSet rs,PreparedStatement st,Connection con) {
		//关闭rs
		if(rs!=null) {
			try {
				rs.close();
				System.out.println("关闭rs成功");
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
		}
		//关闭st
		if(st!=null) {
			try {
				st.close();
				System.out.println("关闭st成功");
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
		}
		//关闭con
		if(con!=null) {
			try {
				con.close();
				System.out.println("关闭con成功");
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
		}
		
	}

}
